<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'" width="600px">
    <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="设备名称">
        <el-input v-model="form.deviceName" class="form_input"/>
      </el-form-item>
      <el-form-item label="设备IP">
        <el-input v-model="form.deviceIp" class="form_input"/>
      </el-form-item>
      <el-form-item label="设备端口">
        <el-input v-model="form.devicePort" class="form_input"/>
      </el-form-item>
      <el-form-item label="节点地址">
        <el-input v-model="form.devicePoint" class="form_input"/>
      </el-form-item>
      <el-form-item label="设备区域">
        <!--<el-input v-model="form.deviceArea" style="width: 370px;"/>-->
        <el-select v-model="form.deviceArea" class="form_input" placeholder="请选择">
          <el-option
            v-for="(item, index) in dict.area"
            :key="item.id"
            :label="item.areaName"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="刷新频率">
        <el-input v-model="form.refreshRate" class="form_input"/>
      </el-form-item>
      <el-form-item label="设备编号">
        <el-input v-model="form.deviceNum" class="form_input"/>
      </el-form-item>
      <el-form-item label="设备类型">
        <el-input v-model="form.deviceType" class="form_input"/>
      </el-form-item>
      <el-form-item label="设备型号">
        <el-input v-model="form.deviceModel" class="form_input"/>
      </el-form-item>
      <el-form-item label="设备级别">
        <el-input v-model="form.deviceGrade" class="form_input"/>
      </el-form-item>
      <el-form-item label="设备品牌">
        <el-input v-model="form.deviceBrand" class="form_input"/>
      </el-form-item>
      <el-form-item label="设备厂商">
        <el-input v-model="form.manufacturer" class="form_input"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { add, edit } from '@/api/environmentalDevice'
  export default {
    props: {
      isAdd: {
        type: Boolean,
        required: true
      },
      sup_this: {
        type: Object,
        default: null
      },
      dict: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        loading: false, dialog: false,
        form: {
          id: '',
          deviceName: '',
          deviceIp: '',
          devicePort: '',
          devicePoint: '',
          deviceArea: '',
          refreshRate: '',
          deviceNum: '',
          deviceType: '',
          deviceModel: '',
          deviceGrade: '',
          deviceBrand: '',
          manufacturer: '',
          creator: '',
          createTime: '',
          updator: '',
          updateTime: ''
        }, rules: {
          deviceName: [
            {required: true, message: '请输入设备名称', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      cancel() {
        this.resetForm()
      },
      doSubmit() {
        this.loading = true
        if (this.isAdd) {
          this.doAdd()
        } else this.doEdit()
      },
      doAdd() {
        add(this.form).then(res => {
          this.resetForm()
          this.$notify({
            title: '添加成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.sup_this.init()
        }).catch(err => {
          this.loading = false
          console.log(err.response.data.message)
        })
      },
      doEdit() {
        edit(this.form).then(res => {
          this.resetForm()
          this.$notify({
            title: '修改成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.sup_this.init()
        }).catch(err => {
          this.loading = false
          console.log(err.response.data.message)
        })
      },
      resetForm() {
        this.dialog = false
        this.$refs['form'].resetFields()
        this.form = {
          id: '',
          deviceName: '',
          deviceIp: '',
          devicePort: '',
          devicePoint: '',
          deviceArea: '',
          refreshRate: '',
          deviceNum: '',
          deviceType: '',
          deviceModel: '',
          deviceGrade: '',
          deviceBrand: '',
          manufacturer: '',
          creator: '',
          createTime: '',
          updator: '',
          updateTime: ''
        }
      }
    }
  }
</script>

<style scoped>
  .form_input {
    width: 180px;
  }

  .form_input_row {
    width: 455px;
  }
</style>
